import axios from 'axios';
import router from '@/router';
import store from '@/store';
import nav from '@/utils/globle.js';
const host = nav.api_host;
var service = axios.create({
  baseURL: host,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
  }
});
service.interceptors.response.use(function (response) {
    if(response.data.code == "401") {
        router.push({name: 'login'});
    }
    return response.data;
  }, function (error) {
    return Promise.reject(error);
  })
service.interceptors.request.use(function (config) {
  
  let token = store.getters.token;
  if(token) {
      config.headers['token'] = token;
  }
  if(config.method.toLowerCase() =='get') {
    config.params = {
      '_t': new Date().getTime(),
      ...config.params,
    }
  } else {
    config.data = {
      '_t': new Date().getTime(),
      ...config.data,
    }
  }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });

export default service;

const Lservice = axios.create({
  baseURL: window.location.origin,
})

Lservice.interceptors.response.use(function (response) {
    return response.data;
  }, function (error) {
    return Promise.reject(error);
  })
export {Lservice};

const Tservice = axios.create({
    baseURL: host,
    timeout: 10000,
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
    }
})

Tservice.interceptors.request.use(function (config) {
  let token = store.getters.token;
  if(token)
  {
    config.headers['token'] = token;
  }
  if(config.method.toLowerCase() =='get') {
    config.params = {
      '_t': new Date().getTime(),
      ...config.params,
    }
  } else {
    config.data = {
      '_t': new Date().getTime(),
      ...config.data,
    }
  }
    return config;
  
  }, function (error) {
    return Promise.reject(error);
  });
Tservice.interceptors.response.use(function (response) {
    return response.data;
  }, function (error) {
    return Promise.reject(error);
  })
export {Tservice};
